package com.huaiyi.fms.open.authorization;

import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SmUtil;

import java.util.logging.Logger;

public class SignUtil {


    private static final Logger log = Logger.getLogger(String.valueOf(SignUtil.class));


    /**
     * 生成签名
     *
     * @param secret appSecret
     * @param data   签名数据 按accessToken,ip地址,接口地址,时间戳(System.currentTimeMillis())的顺序填写
     * @return sign
     */
    public static String buildSign(String secret, String... data) {
        if (data == null || data.length == 0 || data.length > 4) {
            throw new RuntimeException("不合法的签名数据");
        }
        StringBuilder sb = new StringBuilder();
        for (String datum : data) {
            sb.append(datum).append("#");
        }
        String str = sb.substring(0, sb.lastIndexOf("#"));
        String hex = SmUtil.sm4(HexUtil.decodeHex(secret)).encryptBase64(str);
        log.info(hex);
        return hex;
    }


//    public static void main(String[] args) {
//        System.out.println("appKey: " + DESUtil.md5(IdUtil.getSnowflakeNextIdStr()));
//        System.out.println("appSecret: " + HexUtil.encodeHexStr(KeyUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded()));
//        System.out.println("密文:" + SignUtil.buildSign("3a2055435403a2a4c397659e00c38798", "dsfdsfdf545542", "192.168.20.35", "/user/add", "19587655665"));
//        System.out.println("解密：" + SmUtil.sm4(HexUtil.decodeHex("179a8be2cb8da3ccd2964864289601f4")).decryptStr("W2BQYVnWnZxGP80oOpr3X/KBPhKELE3kNlMT48+jpMZtzqyVwA5hBND7FvwY9+32u+/3RJ49w4v02EG4Rqgw/g=="));
//    }

}
